<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * openai使用
 *
 * admin-OpenaiUsage
 *
 * @Apidoc\Title("openai使用")
 * @Apidoc\Group("api")
 */
class OpenaiUsage extends BaseController
{
    /**
     * 新增
     *
     * admin-OpenaiUsage-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\OpenaiUsage::create($data['openai_usage']);
            return success();
        } else {
            $options = [];
            $openai_usage__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['openai_usage__member_id_options'] = $openai_usage__member_id_option;
            $openai_usage__robot_id_option = \app\common\model\Robot::column('name as name, id as id');
            $options['openai_usage__robot_id_options'] = $openai_usage__robot_id_option;
            $openai_usage__robot_conversation_id_option = \app\common\model\RobotConversation::column('name as name, id as id');
            $options['openai_usage__robot_conversation_id_options'] = $openai_usage__robot_conversation_id_option;
            $openai_usage__robot_conversation_message_id_option = \app\common\model\RobotConversationMessage::column('name as name, id as id');
            $options['openai_usage__robot_conversation_message_id_options'] = $openai_usage__robot_conversation_message_id_option;
            $openai_usage__robot_files_id_option = \app\common\model\RobotFiles::column('name as name, id as id');
            $options['openai_usage__robot_files_id_options'] = $openai_usage__robot_files_id_option;
            $openai_usage__type_option = [['name' => '训练', 'id' => '0'], ['name' => '对话', 'id' => '1'], ['name' => 'api对话', 'id' => '2']];
            $options['openai_usage__type_options'] = $openai_usage__type_option;
            $openai_usage__robot_event_id_option = \app\common\model\RobotEvent::column('name as name, id as id');
            $options['openai_usage__robot_event_id_options'] = $openai_usage__robot_event_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-OpenaiUsage-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\OpenaiUsage::update($data['openai_usage']);
            return success();
        } else {
            $data = \app\common\model\OpenaiUsage::find(input('openai_usage__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['openai_usage__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $openai_usage__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['openai_usage__member_id_options'] = $openai_usage__member_id_option;
            $openai_usage__robot_id_option = \app\common\model\Robot::column('name as name, id as id');
            $options['openai_usage__robot_id_options'] = $openai_usage__robot_id_option;
            $openai_usage__robot_conversation_id_option = \app\common\model\RobotConversation::column('name as name, id as id');
            $options['openai_usage__robot_conversation_id_options'] = $openai_usage__robot_conversation_id_option;
            $openai_usage__robot_conversation_message_id_option = \app\common\model\RobotConversationMessage::column('name as name, id as id');
            $options['openai_usage__robot_conversation_message_id_options'] = $openai_usage__robot_conversation_message_id_option;
            $openai_usage__robot_files_id_option = \app\common\model\RobotFiles::column('name as name, id as id');
            $options['openai_usage__robot_files_id_options'] = $openai_usage__robot_files_id_option;
            $openai_usage__type_option = [['name' => '训练', 'id' => '0'], ['name' => '对话', 'id' => '1'], ['name' => 'api对话', 'id' => '2']];
            $options['openai_usage__type_options'] = $openai_usage__type_option;
            $openai_usage__robot_event_id_option = \app\common\model\RobotEvent::column('name as name, id as id');
            $options['openai_usage__robot_event_id_options'] = $openai_usage__robot_event_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-OpenaiUsage-delete
     */
    public function delete()
    {
        \app\common\model\OpenaiUsage::destroy(input('openai_usage__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-OpenaiUsage-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_openai_usage');
        $query->join('db_member', 'db_member.id = db_openai_usage.member_id');
        $query->join('db_robot', 'db_robot.id = db_openai_usage.robot_id');
        if (input('openai_usage__create_time_start', '') || input('openai_usage__create_time_end', '')) {
            $openai_usage__create_time_start = input('openai_usage__create_time_start', '', 'search_in');
            $openai_usage__create_time_end = input('openai_usage__create_time_end', '', 'search_in');
            $openai_usage__create_time_start = $openai_usage__create_time_start ? strtotime($openai_usage__create_time_start) : 0;
            $openai_usage__create_time_end = $openai_usage__create_time_end ? strtotime($openai_usage__create_time_end) + 24 * 60 * 60 - 1 : time();
            $query->where('db_openai_usage.create_time', 'between', [$openai_usage__create_time_start, $openai_usage__create_time_end]);
        }
        if (input('member__id', '') !== '') {
            $query->where('db_member.id', '=', input('member__id', '', 'search_in'));
        }
        if (input('robot__id', '') !== '') {
            $query->where('db_robot.id', '=', input('robot__id', '', 'search_in'));
        }
        $query->where('db_openai_usage.delete_time', '=', null);
        $query->where('db_member.delete_time', '=', null);
        $query->where('db_robot.delete_time', '=', null);
        $query->field(['db_openai_usage.id' => 'openai_usage__id']);
        $query->field(['db_openai_usage.type' => 'openai_usage__type']);
        $query->field(['db_openai_usage.number_of_prompt_words' => 'openai_usage__number_of_prompt_words']);
        $query->field(['db_openai_usage.number_of_response_words' => 'openai_usage__number_of_response_words']);
        $query->field(['db_openai_usage.prompt_unit_price' => 'openai_usage__prompt_unit_price']);
        $query->field(['db_openai_usage.response_unit_price' => 'openai_usage__response_unit_price']);
        $query->field(['db_openai_usage.prompt_money' => 'openai_usage__prompt_money']);
        $query->field(['db_openai_usage.response_money' => 'openai_usage__response_money']);
        $query->field(['db_openai_usage.total_money' => 'openai_usage__total_money']);
        $query->field(['db_openai_usage.apikey' => 'openai_usage__apikey']);
        $query->field(['db_openai_usage.model_name' => 'openai_usage__model_name']);
        $query->field(['db_member.name' => 'member__name']);
        $query->field(['db_robot.name' => 'robot__name']);
        $query->order('db_openai_usage.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        $robot__id_option = \app\common\model\Robot::column('name as name, id as id');
        $options['robot__id_options'] = $robot__id_option;
        $openai_usage__type_option = [['name' => '训练', 'id' => '0'], ['name' => '对话', 'id' => '1'], ['name' => 'api对话', 'id' => '2']];
        $options['openai_usage__type_options'] = $openai_usage__type_option;
        View::assign($options);
        return View::fetch();
    }
}